package com.qf.controller;

import com.qf.model.entity.Patient;
import com.qf.service.PatientService;
import com.qf.util.AliOssUtil;
import com.qf.util.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * (Patient)表控制层
 *
 * @author makejava
 * @since 2024-04-30 09:17:56
 */
@RestController
@RequestMapping("/api/patient")
public class PatientController {
    /**
     * 服务对象
     */
    @Autowired
    private PatientService patientService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("/list/page")
    public R queryByPage(@RequestParam Integer pageNum,
                         @RequestParam Integer pageSize,
                         @RequestParam(required = false) String patientName,
                         @RequestParam(required = false) String idNum
    ) {
        return R.ok(this.patientService.queryByPage(pageNum, pageSize, patientName, idNum));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Long id) {
        return R.ok(this.patientService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param patient 实体
     * @return 新增结果
     */
    @PostMapping("/save")
    public R add(Patient patient) {
        boolean flag = patientService.insert(patient);
        if (flag) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 编辑数据
     *
     * @param patient 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public R edit(Patient patient) {
        int i = patientService.update(patient);
        if (i > 0) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/del/{id}")
    public R deleteById(@PathVariable("id") Long id) {
        boolean flag = this.patientService.deleteById(id);
        if (flag) {
            return R.ok();
        }
        return R.fail();
    }

    @RequestMapping("/del/batch")
    public R delBatch(@RequestParam List<Integer> list) {
        int i = patientService.delBatch(list);
        if (i > 0){
            return R.ok(i);
        }
        return R.fail();
    }

    //上传头像
    @RequestMapping("/tx/upload")
    @ResponseBody
    public R uploadTX(@RequestPart MultipartFile img, Long patientId) throws IOException {
        if (img != null){
            String url = AliOssUtil.uploadByte("java-2401",img.getOriginalFilename(), img.getBytes());
            Patient patient = patientService.queryById(patientId);
            if (patient != null){
                patient.setAvatar(url);
                int i = patientService.update(patient);
                if (i >0){
                    return R.ok();
                }
            }
        }
        return R.fail();

    }

}

